package MapReduce.Demo11_ProductSerch;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/22 11:13
 */
public class ProductSearchCountEasyCombiner extends Reducer<Text,Text,Text,Text> {

    Text outKey = new Text();
    Text outValue = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {

        int sum = 0;

        for (Text value : values) {
            sum += Integer.parseInt(value.toString());
        }

        String productAndFileName = key.toString();
        String[] fields = productAndFileName.split("#");

        outKey.set(fields[0]);
        outValue.set(fields[1]+":"+sum);

        context.write(outKey,outValue);

    }
}
